<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="码头、堆场代码" prop="tmnCode">
        <el-input v-model="formData.tmnCode" placeholder="请输入码头、堆场代码" />
      </el-form-item>
      <el-form-item label="行政区划代码" prop="tmnAddrCode">
        <el-input v-model="formData.tmnAddrCode" placeholder="请输入行政区划代码" />
      </el-form-item>
      <el-form-item label="中文名称" prop="tmnChnname">
        <el-input v-model="formData.tmnChnname" placeholder="请输入中文名称" />
      </el-form-item>
      <el-form-item label="英文名称" prop="tmnEgsname">
        <el-input v-model="formData.tmnEgsname" placeholder="请输入英文名称" />
      </el-form-item>
      <el-form-item label="简称中文" prop="tmnShrchn">
        <el-input v-model="formData.tmnShrchn" placeholder="请输入简称中文" />
      </el-form-item>
      <el-form-item label="客户统一信用代码" prop="customerUscc">
        <el-input v-model="formData.customerUscc" placeholder="请输入客户统一信用代码" />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="formData.customerName" placeholder="请输入客户名称" />
      </el-form-item>
      <el-form-item label="码头、堆场地址" prop="tmnAddress">
        <el-input v-model="formData.tmnAddress" placeholder="请输入码头、堆场地址" />
      </el-form-item>
      <el-form-item label="码头、堆场标志 Y-堆场 T-码头" prop="tmnTyflag">
        <el-input v-model="formData.tmnTyflag" placeholder="请输入码头、堆场标志 Y-堆场 T-码头" />
      </el-form-item>
      <el-form-item label="联系人" prop="tmnContacts">
        <el-input v-model="formData.tmnContacts" placeholder="请输入联系人" />
      </el-form-item>
      <el-form-item label="联系电话" prop="tmnPhone">
        <el-input v-model="formData.tmnPhone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="所属港口" prop="tmnPorts">
        <el-input v-model="formData.tmnPorts" placeholder="请输入所属港口" />
      </el-form-item>
      <el-form-item label="经度" prop="tmnLon">
        <el-input v-model="formData.tmnLon" placeholder="请输入经度" />
      </el-form-item>
      <el-form-item label="纬度" prop="tmnLat">
        <el-input v-model="formData.tmnLat" placeholder="请输入纬度" />
      </el-form-item>
      <el-form-item label="查验费" prop="tmnInspectionFees">
        <el-input v-model="formData.tmnInspectionFees" placeholder="请输入查验费" />
      </el-form-item>
      <el-form-item label="国家代码" prop="tmnCoutry">
        <el-input v-model="formData.tmnCoutry" placeholder="请输入国家代码" />
      </el-form-item>
      <el-form-item label="备注" prop="tmnRemark">
        <el-input v-model="formData.tmnRemark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="机构ID" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入机构ID" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { TmnApi, TmnVO } from '@/api/base/tmn'

/** 码头堆场 表单 */
defineOptions({ name: 'TmnForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  tmnCode: undefined,
  tmnAddrCode: undefined,
  tmnChnname: undefined,
  tmnEgsname: undefined,
  tmnShrchn: undefined,
  customerUscc: undefined,
  customerName: undefined,
  tmnAddress: undefined,
  tmnTyflag: undefined,
  tmnContacts: undefined,
  tmnPhone: undefined,
  tmnPorts: undefined,
  tmnLon: undefined,
  tmnLat: undefined,
  tmnInspectionFees: undefined,
  tmnCoutry: undefined,
  tmnRemark: undefined,
  deptId: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await TmnApi.getTmn(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as TmnVO
    if (formType.value === 'create') {
      await TmnApi.createTmn(data)
      message.success(t('common.createSuccess'))
    } else {
      await TmnApi.updateTmn(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    tmnCode: undefined,
    tmnAddrCode: undefined,
    tmnChnname: undefined,
    tmnEgsname: undefined,
    tmnShrchn: undefined,
    customerUscc: undefined,
    customerName: undefined,
    tmnAddress: undefined,
    tmnTyflag: undefined,
    tmnContacts: undefined,
    tmnPhone: undefined,
    tmnPorts: undefined,
    tmnLon: undefined,
    tmnLat: undefined,
    tmnInspectionFees: undefined,
    tmnCoutry: undefined,
    tmnRemark: undefined,
    deptId: undefined
  }
  formRef.value?.resetFields()
}
</script>